﻿using System;
using System.Collections.Generic;
using System.Data.Entity.Validation;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for PlanningManager
/// </summary>
public class PlanningManager
{
    private debioscoopEntities db;

	public PlanningManager(debioscoopEntities db)
	{
        this.db = db;
	}

    public void addPlanning(planning planning)
    {
        db.plannings.Add(planning);
        executeQuery();
    }

    public planning getPlanning(int planningID)
    {
        planning returnPlanning = db.plannings.FirstOrDefault(b => b.planning_id == planningID);
        return returnPlanning;
    }

    public List<planning> getPlanningByDate(DateTime date)
    {
        return db.plannings.Where(p => p.datum == date.Date).ToList();
    }

    public planning getPlanningMovie(int movieID, DateTime date, TimeSpan movieTime)
    {
        return db.plannings.FirstOrDefault(p => p.filmfilm_id == movieID && p.datum == date.Date && p.tijd == movieTime);
    }

    public int getAvailableSeats(planning tempPlanning)
    {
        int availableSeats = 0;
        int seats = tempPlanning.zaal.zitting;
        int takenSeats = tempPlanning.kaartjes.Count();
        availableSeats = seats - takenSeats;
        return availableSeats;
    }

    public List<planning> getPlanningenMovieByDate(int movieID, DateTime date, bool timeNow = true)
    {
        List<planning> planningen;
        TimeSpan currentTime;

        if (timeNow == true)
        {
            currentTime = new TimeSpan(DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);
            planningen = db.plannings.Where(p => p.filmfilm_id == movieID && p.datum == date.Date && p.tijd > currentTime).OrderBy(p => p.tijd).ToList();
        }
        else
        {
            planningen = db.plannings.Where(p => p.filmfilm_id == movieID && p.datum == date.Date).OrderBy(p => p.tijd).ToList();
        }
        return planningen;
    }

    public List<String> getMovieTimes(film tempFilm, DateTime date)
    {
        TimeSpan[] tempTimes = tempFilm.plannings
            .Where(p => p.datum == date.Date && p.tijd >= DateTime.Now.TimeOfDay)
            .OrderBy(p => p.tijd).Select(p => p.tijd).ToArray();
        List<String> times  = new List<String>();
        foreach (TimeSpan time in tempTimes)
        {
            times.Add(string.Format("{0:00}:{1:00}", time.Hours, time.Minutes));
        }
        return times;
    }

    public bool checkMovieAvailable(int movieID)
    {
        List<planning> planningen = db.plannings.Where(p => p.filmfilm_id == movieID && p.datum >= DateTime.Today.Date).ToList();
        if (planningen.Count > 0)
        {
            return true;
        }
        return false;
    }

    public void executeQuery()
    {
        try
        {
            db.SaveChanges();
        }
        catch (DbEntityValidationException e)
        {
            foreach (var eve in e.EntityValidationErrors)
            {
                System.Diagnostics.Debug.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                    eve.Entry.Entity.GetType().Name, eve.Entry.State);
                foreach (var ve in eve.ValidationErrors)
                {
                    System.Diagnostics.Debug.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                        ve.PropertyName, ve.ErrorMessage);
                }
            }
            throw;
        }
        catch (Exception e)
        {
            System.Diagnostics.Debug.WriteLine(e.InnerException);
        }
    }
}